/*
 * ip.cpp
 *
 *  Created on: Jan 18, 2016
 *      Author: root
 */

#include<iostream>
#include<string.h>
#include<vector>
#include<sstream>
#include<unistd.h>
#include<stdlib.h>

#include "string.h"
#include "package.h"

using namespace dounine_string;
using namespace dounine_package;
using namespace std;



namespace dounine_ip{
	#define SYN_PORT 8080
	#define POINT '.'
    #define sleep_create_ip 1000000

	void create_ip(const string& ip,const string& source_ip){
		usleep(sleep_create_ip);
		dounine_package::send_syn_package(ip,source_ip,SYN_PORT);
	}

	void spl_ip(const string& ip,const string& source_ip){
			vector<string> ipLine = dounine_string::split(ip,"\t");

			if(ipLine[0]=="#")return;

			string start = ipLine[1];
			string end = ipLine[2];

			vector<string> startArr = dounine_string::split(start,".");
			vector<string> endArr = dounine_string::split(end,".");

			int sta[] = {atoi(startArr[0].c_str()),atoi(startArr[1].c_str()),atoi(startArr[2].c_str()),atoi(startArr[3].c_str())};
			int ena[] = {atoi(endArr[0].c_str()),atoi(endArr[1].c_str()),atoi(endArr[2].c_str()),atoi(endArr[3].c_str())};
			stringstream io;
			for(int i1 = sta[0];i1<=ena[0];++i1){
				for(int i2 = (sta[1]==0?1:sta[1]);i2<=ena[1];++i2){
					for(int i3 = (sta[2]==0?1:sta[2]);i3<=ena[2];++i3){
						for(int i4 = (sta[3]==0?1:sta[3]);i4<=ena[3];++i4){
							io<<i1<<POINT<<i2<<POINT<<i3<<POINT<<i4;
							create_ip(io.str(),source_ip);
							io.str("");
						}
					}
				}
			}
		}

}
